
//
//  体温折线图
//
//  Created by Robin-Pc on 2017/5/7.
//  Copyright © 2017年 Robin-Pc. All rights reserved.
//

#import "BMIViewController.h"
#import "ARLineChartView.h"
#import "ARLineChartCommon.h"
#import "TimeTools.h"

#define URL_Getheight @"/api/dailyIndicator/myheight"
#define URL_Getweight @"/api/dailyIndicator/myweight"

@interface BMIViewController ()
@property(nonatomic, strong)NSDictionary *weightDic;//体重
@property(nonatomic, strong)NSDictionary *heightDic;//身高
@property(nonatomic, strong)ARLineChartView *lineChartView;
@end

@implementation BMIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[YZENetwork sharedInstace] getRequestWithUrl:[BASE stringByAppendingString:URL_Getweight] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
        self.weightDic = [BMIViewController getPresureArr:responseObject];
        [self calculateAndDraw];
    }];
    [[YZENetwork sharedInstace] getRequestWithUrl:[BASE stringByAppendingString:URL_Getheight] parameters:nil delegate:self requestTag:2 successBlock:^(id responseObject) {
        self.heightDic = [BMIViewController getPresureArr:responseObject];
        [self calculateAndDraw];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    if (!self.lineChartView) {
        return;
    }
    [self.lineChartView removeFromSuperview];
    self.lineChartView = nil;
    
    [[YZENetwork sharedInstace] getRequestWithUrl:[BASE stringByAppendingString:URL_Getweight] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
        self.weightDic = [BMIViewController getPresureArr:responseObject];
        [self calculateAndDraw];
    }];
    [[YZENetwork sharedInstace] getRequestWithUrl:[BASE stringByAppendingString:URL_Getheight] parameters:nil delegate:self requestTag:2 successBlock:^(id responseObject) {
        self.heightDic = [BMIViewController getPresureArr:responseObject];
        [self calculateAndDraw];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 业务逻辑
//计算数据并绘图
- (void)calculateAndDraw{
    if (!self.weightDic || !self.heightDic) {
        return;
    }
    
    /*/构造测试数据
    self.weightDic = @{@1512950400:@62, @1513126800:@60};
    self.heightDic = @{@1512950400:@162, @1513126800:@170};
    ////*/
    
    NSMutableArray *dataArr = [NSMutableArray array];
    NSMutableArray *t_weight_date_arr = [[self.weightDic allKeys] mutableCopy];
    NSMutableArray *t_height_date_arr = [[self.heightDic allKeys] mutableCopy];
    if (t_weight_date_arr.count==0 || t_height_date_arr.count==0) {
        return;
    }
    [t_weight_date_arr sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return ((NSNumber*)obj1)<((NSNumber*)obj2);
    }];
    [t_height_date_arr sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return ((NSNumber*)obj1)<((NSNumber*)obj2);
    }];
    NSEnumerator *en = [t_weight_date_arr objectEnumerator];
    NSNumber *date_num;
    NSInteger height_index = 0;
    
    while (date_num = [en nextObject]) {
        NSString *h = [self.heightDic objectForKey:t_height_date_arr[height_index]];
        NSString *w = [self.weightDic objectForKey:date_num];
        
        if (h && w) {
            double hd = [h doubleValue];
            double wd = [w doubleValue];
            RLLineChartItem *point = [[RLLineChartItem alloc]init];
            point.xValue = [date_num doubleValue];
            point.y1Value = (wd*10000)/(hd*hd);
            [dataArr addObject:point];
            
            if (self.heightDic[date_num]) {
                height_index++;
            }
        }
    }
    
    //折线图view
    CGRect rect = CGRectMake (5, 40, self.view.frame.size.width - 5 - 5, 300);
    self.lineChartView = [[ARLineChartView alloc] initWithFrame:rect
                                                     dataSource:dataArr
                                                         isDual:NO
                                                         xTitle:@"检查时间"
                                                        y1Title:@"身高" y2Title:@"nonsense"
                                                          desc1:@"nonsense" desc2:@"nonsense"
                                                        xLimits:@[]];
    [self.view addSubview:self.lineChartView];
}

//整理服务器返回数据
+ (NSArray*)convertServerData2LocalArr:(NSArray*)obj{
    NSDictionary *heightDic = [BMIViewController getPresureArr:obj];
    NSArray *dateArr = [heightDic allKeys];//日期数组
    NSMutableArray *dataArr = [NSMutableArray array];
    for (NSNumber *datekey in dateArr) {
        RLLineChartItem *point = [[RLLineChartItem alloc]init];
        point.xValue = [datekey doubleValue];
        NSString *val = [heightDic objectForKey:datekey];
        if (val) {
            point.y1Value = [val doubleValue];
        }
        [dataArr addObject:point];
    }
    
    return dataArr;
}

+(NSDictionary*)getPresureArr:(NSArray*)rawArr{
    NSMutableDictionary *presureArr = [NSMutableDictionary dictionary];
    for (NSDictionary *item in rawArr) {
        NSArray *keys = [item allKeys];
        if (keys.count==0) {
            continue;
        }
        NSString *datekey = keys[0];
        NSString *val = item[datekey];
        NSDate *date = [TimeTools getDateWithString:datekey];
        long timeVal = [date timeIntervalSince1970];
        if ([val isNotEmpty]) {
            [presureArr setObject:val  forKey:[NSNumber numberWithLong:timeVal]];
        }
    }
    return presureArr;
}

+(BOOL)compareDateString:(NSString*)date_str1 with:(NSString*)date_str2{
    NSDate *d1 = [TimeTools getDateWithString:date_str1];
    NSTimeInterval t1 = [d1 timeIntervalSinceReferenceDate];
    NSDate *d2 = [TimeTools getDateWithString:date_str2];
    NSTimeInterval t2 = [d2 timeIntervalSinceReferenceDate];
    return t1<=t2;
}

@end
